package com.example.xpvideo.Controller;

import com.example.xpvideo.annotations.IsFile;
import com.example.xpvideo.entity.LsCarouselPicture;
import com.example.xpvideo.entity.LsDataCarousel;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.service.LsCarouselPictureService;
import com.example.xpvideo.service.LsDataCarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author hjt
 * @create 2021-08-28 14:13
 * @describe：首页轮播配置
 */
@RestController
@RequestMapping("carousel")
public class CarouselPictureController {

    @Autowired
    private LsCarouselPictureService carouselPictureService;
    @Autowired
    private LsDataCarouselService dataCarouselService;


    /**
     * 所有查询轮播图
     *
     * @return
     */
    @GetMapping("queryCarouselPicture")
    public ResponseResult queryCarouselPicture() {
        List<LsCarouselPicture> list = carouselPictureService.list();
        return ResponseResult.responseResult(0, list);
    }

    /**
     * 添加轮播图
     * @return
     */
    @PostMapping("addCarouselPicture")
    public ResponseResult addCarouselPicture(@Validated @IsFile MultipartFile file) {
        return carouselPictureService.addImages(file, null);
    }

    /**
     * 更换轮播图
     * @return
     */
    @PutMapping("updateCarouselPicture")
    public ResponseResult updateCarouselPicture(@Validated @IsFile MultipartFile file, @RequestParam("id") Integer id) {
        return carouselPictureService.addImages(file, id);
    }

    /**
     * 删除添加轮播图
     *
     * @return
     */
    @DeleteMapping("delCarouselPicture")
    public ResponseResult delCarouselPicture(@RequestParam("id") Integer id) {
        return carouselPictureService.delCarouselPicture(id);
    }


    /**
     * 查找所有首页轮播文字
     * @return
     */
    @GetMapping("findAllCarouselData")
    public ResponseResult findAllCarouselData() {
        List<LsDataCarousel> list = dataCarouselService.list();
        return ResponseResult.responseResult(0, list);
    }

    /**
     * 添加轮播文字
     * @return
     */
    @PostMapping("addDataCarousel")
    public ResponseResult addDataCarousel(@Validated @RequestBody LsDataCarousel dataCarousel, @Validated @IsFile MultipartFile file, BindingResult result) {
        if (result.hasErrors()) {
            return ResponseResult.responseResult(ErrCodeEnum.unknown_10001, result.getTarget());
        }
        return dataCarouselService.addDataCarousel(dataCarousel, file);
    }


    /**
     * 删除某条轮播文本
     * @return
     */
    @DeleteMapping("delDataCarousel/{id}")
    public ResponseResult delDataCarousel(@PathVariable("id") Integer id) {
        return dataCarouselService.delDataCarousel(id);
    }




}
